$(function () {
    $.get("../sysoffice/tree", function(r){
        // dataTree = r;
        ztree = $.fn.zTree.init($("#menuTree"), setting, r.list);
        //展开所有节点
        ztree.expandAll(true);

        /*if(roleId != null){
         vm.getRole(roleId);
         }*/
    });
    $("#jqGrid").jqGrid({
        url: '../fybx/list',
        datatype: "json",
        colModel: [			
			{ label: 'bxId', name: 'bxId', index: 'bx_id', width: 50, key: true ,hidden:true},
            { label: '流程状态', name: 'pistatus', index: 'pistatus', width: 130 ,formatter:pistatus},
            { label: '姓名', name: 'userdesc', index: 'userdesc', width: 80 },
            { label: '结算点', name: 'settlementpoint', index: 'settlementpoint', width: 130 ,formatter:getDictInfo},
            { label: '附件id', name: 'fileId', index: 'file_id', width: 80 ,hidden:true},
			{ label: '报销部门', name: 'bxbm', index: 'bxbm', width: 80 }, 			
			{ label: '报销部门id', name: 'bxbmid', index: 'bxbmid', width: 80 ,hidden:true },
			{ label: '合计金额', name: 'hjje', index: 'hjje', width: 80 }, 			
			{ label: '原借款', name: 'yjk', index: 'yjk', width: 80 }, 			
			{ label: '应退(补)款', name: 'ytbk', index: 'ytbk', width: 80 }, 			
			{ label: '备注', name: 'bz', index: 'bz', width: 80 },
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80 ,formatter:getDictInfo},
			{ label: '报销人', name: 'bxr', index: 'bxr', width: 80  ,hidden:true},
			{ label: '报销人id', name: 'bxrid', index: 'bxrid', width: 80  ,hidden:true},
			{ label: '提交日期', name: 'tjDate', index: 'tj_date', width: 80 },
            { label: '流程指向', name: 'nextTo', index: 'next_to', width: 80 ,hidden:true},
            { label: '审批/抄送人', name: 'nextUser', index: 'next_user', width: 90 },
            { label: '流程ID', name: 'piid', index: 'piid', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getDetinfo();
    var r = utils.getDictList("setlever");
    vm.setlever = r.setlever;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable:false,
		title: null,
		fybx: {},
        fybxDetList:[],
		q:{
			bxid:null
		},
        setlever:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function() {
            vm.showList = false;
            vm.showtable = false;
            vm.title = "新增";
            vm.fybx = {
                bxbmid:null
            };
            vm.fybxDetList=[];
            vm.fybxDetList.push({
                bxXm:"",
                sjfyXm: "",
                zy:"",
                bxJe:null,
                bz:""
            });

        },
		update: function (event) {
			var bxId = getSelectedRow();
			if(bxId == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",bxId);
            if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
            {
                alert("数据已提交，不能修改");
                return ;
            }
			vm.showList = false;
            vm.showtable = false;
            vm.title = "修改";

            vm.getInfo(bxId)
		},
		saveOrUpdate: function (event) {
			console.log(vm.fybx);
			if(vm.fybxDetList==undefined||vm.fybxDetList.length==0){
				alert("最少要有一行明细数据！");
                return;
			}
            var params={"fybxDetList":vm.fybxDetList,
                "fybx":vm.fybx
            };
			var url = vm.fybx.bxId == null ? "../fybx/save" : "../fybx/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(params),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var bxIds = getSelectedRows();
			if(bxIds == null){
				return ;
			}
            var len=bxIds.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",bxIds[i]);
                if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
                {
                    alert("数据已提交，不能删除");
                    return ;
                }
            }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../fybx/delete",
				    contentType: "application/json",
				    data: JSON.stringify(bxIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(bxId){
			$.get("../fybx/info/"+bxId, function(r){
                vm.fybx = r.map.fybx;
                vm.fybxDetList = r.map.fybxDetList;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        addCertificateItem: function(){
            vm.fybxDetList.push({
                bxXm:"",
                sjfyXm: "",
                zy:"",
                bxJe:null,
                bz:""
            });
        },
        delCertificateItem: function(index){
            vm.fybxDetList.splice(index,1);
        },
        getDetinfo: function(){
            $("#jqGridTable").jqGrid({
                url: '../fybxdet/list',
                datatype: "local",
                colModel: [
                    { label: 'bxdetId', name: 'bxdetId', index: 'bxdet_id', width: 50, key: true, hidden:true},
                    { label: '报销id', name: 'bxId', index: 'bx_id', width: 80 , hidden:true},
                    { label: '报销项目', name: 'bxXm', index: 'bx_xm', width: 80 },
                    { label: '实际费用项目', name: 'sjfyXm', index: 'sjfy_xm', width: 80 },
                    { label: '摘要', name: 'zy', index: 'zy', width: 80 },
                    { label: '备注', name: 'bz', index: 'bz', width: 80 },
                    { label: '报销金额', name: 'bxJe', index: 'bx_je', width: 80 }
                ],
                viewrecords: true,
                height: 300,
                width: 500,
                rowNum: 5,
                rowList : [10,30,50],
                rownumbers: true,
                autowidth:false,
                //字段太多自动加载横轴滚动条
              /*  shrinkToFit:false,
                autoScroll: false,
                multiselect:false,*///只允许单选
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                }
            });
        },
        reloadDetinfo: function (event) {
            var page = $("#jqGridTable").jqGrid('getGridParam','page');
            $("#jqGridTable").jqGrid('setGridParam',{
                datatype:'json',
                postData : {bxid : vm.q.bxid},
                page:page
            }).trigger("reloadGrid");
            utils.openDialogView('报销明细',$("#window"),'550px','400px',1);
        },
        querydet:function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.q.bxid=id;
            vm.reloadDetinfo();
		},
        selectunit:function(){
            openDialogTreeView('选择部门',$("#windowtree"),'400px','450px',1);
        },
        queryfile:function(){
            var Id = getSelectedRow();
            if(Id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", Id);
            utils.openDialogView('上传附件',"../sys/upload.html?fileId="+rowData.fileId,"900px","500px",2);
        },


        submit: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid!=0&&piid!=null&&piid!=""){
                alert("数据已提交，不能重复提交！");
                return;
            }
            var url = "../fybx/submit";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        processInt: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.title = "审批";

            vm.getInfo(id)
        },

        process: function (event) {
            var url = "../fybx/process";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.fybx),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reject: function (event) {
            var url = "../fybx/reject";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.fybx),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        popHistory: function(){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid==0||piid==null||piid==""){
                alert("没有流程！");
                return;
            }
            utils.popHistory(piid);
            var page = $("#popHistoryGrid").jqGrid('getGridParam','page');
            $("#popHistoryGrid").jqGrid('setGridParam',{
                postData:{
                    'piid': piid,
                },
                page:page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "历史记录",
                area: ['725px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popHistory"),
                /*  btn: ['确定', '取消'],
                 btn1: function (index) {
                 layer.close(index);
                 }*/
            });
        },


	}
});
//打开一颗树
openDialogTreeView=function(title,url,width,height,type){
    if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
        width='auto';
        height='auto';
    }else{//如果是PC端，根据用户设置的width和height显示。

    }
    layer.open({
        type: type,
        offset: '50px',
        skin: 'layui-layer-molv',
        title: title,
        area: [width, height],
        maxmin: true, //开启最大化最小化按钮
        content: url,
        btn: ['确定','关闭'],
        btn1:function(index, layero){
            if(treeLine==undefined){
                alert("请选择部门！");
                return;
            }
            vm.fybx.bxbmid=treeLine.id;
            vm.fybx.bxbm=treeLine.name;
            layer.close(index);
        },
        btn2:function(index){
            layer.close(index);
        }
    });
}
getCurrentNode=function(treeId, treeNode){

}
var treeLine;
zTreeOnClick=function(event, treeId, treeNode) {
    treeLine=treeNode;
}
var setting = {
    data: {
        simpleData: {
            enable: false,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: "id"
        },
        key: {
            url:"nourl"
        }
    },
    check:{
        enable:false,
        nocheckInherit:true
    },
    callback:{
        //beforeClick: getCurrentNode,
        onClick : zTreeOnClick
    }
};